<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
    require([ "dojo/_base/array", "dojo/_base/xhr", "dojo/_base/lang", "dojo/dom", "dojo/request", "dojo/topic", "dojo/json" ],
            function(array, xhr, lang, dom, request, topic, JSON) {
                var deletItemsCount = 0;
                holidayrecordRestForm = function(formId) {
                    dijit.byId(formId).reset();
                    if (formId.indexOf('UFD') != -1) {
                        var rowid = jQuery("#holidayrecordGird").jqGrid('getGridParam', 'selrow');
                        var holidayrecord = jQuery("#holidayrecordGird").jqGrid('getRowData', rowid);
                        dijit.byId('holidayrecordUFD').setValues(holidayrecord);
                        dom.byId("starttimeUFD_WP").value = holidayrecord.starttime;
                        dom.byId("endtimeUFD_WP").value = holidayrecord.endtime;
                    } else {
                        dom.byId("starttimeIFD_WP").value = "";
                        dom.byId("endtimeIFD_WP").value = "";
                    }
                };
                topic.subscribe("holidayrecord/insertOrUpdate", holidayrecordRestForm);
                var syncDB = function(url, jsonData) {
                    request.post(url, {
                        data : jsonData,
                        headers : {
                            'Content-Type' : "application/json; charset=utf-8"
                        },
                        timeout : 30000
                    }).then(function(response) {
                        topic.publish("notifyMsgTopic", {
                            message : response,
                            type : "message",
                            duration : 1000
                        });
                        var currentPage = jQuery('#holidayrecordGird').jqGrid('getGridParam', 'page');
                        var pageSize = jQuery('#holidayrecordGird').jqGrid('getGridParam', 'rowNum');
                        var totalCount = jQuery('#holidayrecordGird').jqGrid('getGridParam', 'records') - deletItemsCount;
                        var totalPages = Math.ceil(totalCount / pageSize);
                        jQuery("#holidayrecordGird").jqGrid('setGridParam', {
                            page : currentPage > totalPages ? totalPages : currentPage
                        }).trigger("reloadGrid");
                    }, function(error) {
                        alert(error);
                    });
                };
                holidayrecordSubmitForm = function(formId, type) {
                    var url = "holidayrecord/insert";
                    var starttime, endtime;
                    if (type == 'u') {
                        url = "holidayrecord/update";
                        starttime = dom.byId("starttimeUFD_WP").value;
                        endtime = dom.byId("endtimeUFD_WP").value;
                    } else {
                        starttime = dom.byId("starttimeIFD_WP").value;
                        endtime = dom.byId("endtimeIFD_WP").value;
                    }
                    if((!starttime || starttime=='') || (!endtime || endtime=='') ){
                        alert('开始结束时间不能为空');
                        return;
                    }
                    var form = dijit.byId(formId);
                    if (form.isValid()) {
                        var formData = form.getValues();
                        lang.mixin(formData, {
                            starttime : starttime,
                            endtime : endtime
                        });
                        var json = JSON.stringify(formData);
                        syncDB(url, json);
                        form.hide();
                        topic.publish("holidayrecord/insertOrUpdate", formId);
                    } else {
                        form.validate();
                    }
                };
                holidayrecordDelete = function() {
                    var ids = jQuery("#holidayrecordGird").jqGrid('getGridParam', 'selarrrow') || [];
                    deletItemsCount = ids.length;
                    if (ids.length > 0) {
                        if (confirm('确认删除所选定的项？')) {
                            var holidayrecord = {};
                            holidayrecord.id = '';
                            array.forEach(ids, function(entry, i) {
                                holidayrecord.id += entry + ",";
                            });
                            syncDB("holidayrecord/delete", JSON.stringify(holidayrecord));
                        }
                    } else {
                        alert('请选择至少一条数据!');
                    }
                };
                holidayrecordQuery = function(formId) {
                    var starttime = dom.byId("starttimeQFD_WP").value;
                    var endtime = dom.byId("endtimeQFD_WP").value;
                    var holidayrecord = dijit.byId(formId).getValues();
                    lang.mixin(holidayrecord, {
                        starttime : starttime,
                        endtime : endtime
                    });
                    jQuery("#holidayrecordGird").jqGrid('setGridParam', {
                        postData : holidayrecord,
                        mtype : "post"
                    }).trigger('reloadGrid');
                };
                changeOpt = function(newValue){
                  if(newValue == "1") {
                      dijit.byId("fullnameIFD").set("disabled", true);
                      dijit.byId("fullnameUFD").set("disabled", true);
                  } else {
                      dijit.byId("fullnameIFD").set("disabled", false);
                      dijit.byId("fullnameUFD").set("disabled", false);
                  }
                };
            });
</script>
<div data-dojo-type="dijit/Dialog" id="holidayrecordIFD" title="新增">
  <div class="dijitDialogPaneContentArea">
    <table>
      <tr>
        <td><label for="starttime">开始日期: </label></td>
        <td>
        <input id="starttimeIFD_WP" class="Wdate" type="text" onFocus="var d5222=$dp.$('endtimeIFD_WP');WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',onpicked:function(){d5222.focus();},maxDate:'#F{$dp.$D(\'endtimeIFD_WP\')}'})"/>
        <input data-dojo-type="dijit/form/TextBox" name="starttime" id="starttimeIFD" style="display: none;">
        </td>
        <td><label for="endtime">结束日期: </label></td>
        <td>
        <input id="endtimeIFD_WP" class="Wdate" type="text" onFocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',minDate:'#F{$dp.$D(\'starttimeIFD_WP\')}'})"/>
        <input data-dojo-type="dijit/form/TextBox" name="endtime" id="endtimeIFD" style="display: none;">
        </td>
      </tr>
      <tr>
        <td><label for="descp">假期描述: </label></td>
        <td colspan="3"><textarea data-dojo-type="dijit/form/SimpleTextarea" name="descp" rows="4" cols="50" style="width:auto;"></textarea></td>
      </tr>
      <tr>
        <td><label for="isall">全公司假期：</label></td>
        <td>
          <select data-dojo-type="dijit/form/Select" name="isall" data-dojo-props="required: true,onChange:changeOpt" style="width: 182px;">
            <option value="0">否</option>
            <option value="1">是</option>
          </select>
        </td>
        <td><label for="userid">所属用户：</label></td>
        <td>
          <input id="fullnameIFD" name="fullname" data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true,readonly : true" cType="s" onclick="showUserTree(this,'useridIFD');" />
          <input id="useridIFD" name="userid" data-dojo-type="dijit/form/TextBox" data-dojo-props="trim:true" style="display: none;" >
        </td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="holidayrecordSubmitForm('holidayrecordIFD','i')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="holidayrecordRestForm('holidayrecordIFD');">重置</button>
  </div>
</div>
<div data-dojo-type="dijit/Dialog" id="holidayrecordUFD" title="更新">
  <div class="dijitDialogPaneContentArea">
    <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, readOnly: true" type="text"
      name="recordid" style="display: none;">
    <table>
      <tr>
        <td><label for="starttime">开始日期: </label></td>
        <td>
        <input id="starttimeUFD_WP" class="Wdate" type="text" onFocus="var d5222=$dp.$('endtimeUFD_WP');WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',onpicked:function(){d5222.focus();},maxDate:'#F{$dp.$D(\'endtimeUFD_WP\')}'})"/>
        <input data-dojo-type="dijit/form/TextBox" name="starttime" id="starttimeUFD" style="display: none;">
        </td>
        <td><label for="endtime">结束日期: </label></td>
        <td>
        <input id="endtimeUFD_WP" class="Wdate" type="text" onFocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',minDate:'#F{$dp.$D(\'starttimeUFD_WP\')}'})"/>
        <input data-dojo-type="dijit/form/TextBox" name="endtime" id="endtimeUFD" style="display: none;">
        </td>
      </tr>
      <tr>
        <td><label for="descp">假期描述: </label></td>
        <td colspan="3"><textarea data-dojo-type="dijit/form/SimpleTextarea" name="descp" rows="4" cols="50" style="width:auto;"></textarea></td>
      </tr>
      <tr>
        <td><label for="isall">全公司假期：</label></td>
        <td>
          <select data-dojo-type="dijit/form/Select" name="isall" data-dojo-props="required: true,onChange:changeOpt" style="width: 182px;">
            <option value="0">否</option>
            <option value="1">是</option>
          </select>
        </td>
        <td><label for="userid">所属用户：</label></td>
        <td>
          <input id="fullnameUFD" name="fullname" data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true,readonly : true" cType="s" onclick="showUserTree(this,'useridUFD');" />
          <input id="useridUFD" name="userid" data-dojo-type="dijit/form/TextBox" data-dojo-props="trim:true" style="display: none;" >
        </td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="holidayrecordSubmitForm('holidayrecordUFD','u')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="holidayrecordRestForm('holidayrecordUFD');">重置</button>
  </div>
</div>
<fieldset>
  <legend>&nbsp;&nbsp;信息选项&nbsp;&nbsp;</legend>
  <form id="queryHolidayRecordForm" data-dojo-type="dijit/form/Form" method="post">
    <div style="margin-left: 5px;">
      <table>
        <tr>
        <td><label for="starttime">开始日期: </label></td>
        <td>
        <input id="starttimeQFD_WP" name="starttime" class="Wdate" type="text" onFocus="var d5222=$dp.$('endtimeQFD_WP');WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',onpicked:function(){d5222.focus();},maxDate:'#F{$dp.$D(\'endtimeQFD_WP\')}'})"/>
        </td>
        <td><label for="endtime">结束日期: </label></td>
        <td>
        <input id="endtimeQFD_WP" class="Wdate" name="endtime" type="text" onFocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',minDate:'#F{$dp.$D(\'starttimeQFD_WP\')}'})"/>
        </td>
      </tr>
      </table>
    </div>
  </form>
  <div style="padding: 5px;">
    <button id="holidayrecordInsertBtn" data-dojo-type="dijit/form/Button" type="submit" label="新增">
      <script type="dojo/method" event="onClick" args="evt">
        dijit.byId('holidayrecordIFD').show();
      </script>
    </button>
    <button id="holidayrecordDeleteBtn" data-dojo-type="dijit/form/Button" type="button" label="删除" onclick="holidayrecordDelete();"></button>
    <button id="holidayrecordUpdateBtn" data-dojo-type="dijit/form/Button" type="button" label="更新">
      <script type="dojo/method" event="onClick" args="evt">
        var ids = jQuery("#holidayrecordGird").jqGrid('getGridParam', 'selarrrow') || [];
        if (ids.length != 1) {
          alert('每次只能修改一条数据!');
        }else{
          var holidayrecord = jQuery("#holidayrecordGird").jqGrid('getRowData', ids[0]);
          dijit.byId('holidayrecordUFD').setValues(holidayrecord);
          dojo.byId("starttimeUFD_WP").value = holidayrecord.starttime;
          dojo.byId("endtimeUFD_WP").value = holidayrecord.endtime;
          dijit.byId('holidayrecordUFD').show();
        }
      </script>
    </button>
    <button id="holidayrecordQueryBtn" data-dojo-type="dijit/form/Button" type="button" label="查询"
      onclick="holidayrecordQuery('queryHolidayRecordForm');"></button>
  </div>
</fieldset>
<div id="gird">
  <table id="holidayrecordGird"></table>
  <div id="holidayrecordToolbar"></div>
</div>
<script type="text/javascript">
    var isallMap = { "0" : "否", "1" : "是" };
    jQuery("#holidayrecordGird").jqGrid({
        url : 'holidayrecord/queryAsList',
        datatype : "json",
        autowidth : true,
        rownumbers : true,
        height : 'auto',
        colNames : [
          '记录ID',
          '开始日期',
          '结束日期',
          '假期描述',
          '用户名',
          '所属用户',
          '全公司假期ID',
          '全公司假期'
        ],
        colModel : [ 
        {
          name : 'recordid',
          index : 'recordid',
          width : 60,
          key : true,
          hidden : true,
          align : 'center'
        },
        {
          name : 'starttime',
          index : 'starttime'
        },
        {
          name : 'endtime',
          index : 'endtime'
        },
        {
          name : 'descp',
          index : 'descp'
        },
        {
          name : 'fullname',
          index : 'fullname'
        },
        {
          name : 'userid',
          index : 'userid',
          hidden : true
        },
        {
          name : 'isall',
          index : 'isall',
          hidden : true
        },
        {
          name : 'isallName',
          index : 'isallName',
          align : 'center'
        }
        ],
        rowNum : 15,
        rowList : [ 15, 20, 25, 30 ],
        pager : '#holidayrecordToolbar',
        recordpos : 'right',
        viewrecords : true,
        sortorder : "desc",
        jsonReader : {
            repeatitems : false
        },
        gridComplete : function() {
            var holidayrecordGird = jQuery("#holidayrecordGird");
            var ids = holidayrecordGird.jqGrid('getDataIDs');
            for ( var i = 0; i < ids.length; i++) {
                var cl = ids[i];
                var holidayrecord = holidayrecordGird.jqGrid('getRowData', cl);
                var isall = holidayrecord.isall;
                holidayrecordGird.jqGrid('setRowData', cl, {
                    isallName : isallMap[isall]
                });                
            }
        },
        multiselect : true,
        shrinkToFit : false,
        caption : "放假记录列表"
    });
    jQuery("#holidayrecordGird").jqGrid('navGrid', '#holidayrecordToolbar', {
        edit : false,
        add : false,
        del : false,
        search : false,
        refresh : true
    });
</script>
